package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
import com.db4o.query.QueryComparator;

public class OrderListView extends Composite {
	public static int EVT_ORDER_SELETED = 0x7000;
	
	private static int MAX_CHOICE_PER_PAGE = 15;
	
	private static int HORIZONTAL_PADDING = 10;
	private static int X_GAP = 5;
	private static int BUTTON_DIM = 90;
	
	private boolean inResize = false;
	
	private Button[] choices = new Button[MAX_CHOICE_PER_PAGE];
	
	private int choicePerPage;
	private int currentPage;
	
	private Order selectedOrder;
	
	public OrderListView(Composite parent) {
		super(parent, SWT.NONE);
		
		setLayout(new MigLayout("",""));

		for (int i=0; i<MAX_CHOICE_PER_PAGE; ++i) {
			choices[i] = new Button(this, SWT.TOGGLE);
			choices[i].setText("");
			choices[i].setVisible(false);
			choices[i].setLayoutData("hidemode 3");
			
			choices[i].addSelectionListener(new SelectionAdapter() {

				@Override
				public void widgetSelected(SelectionEvent e) {
					Button b = (Button) e.widget;
					
					for (Button btn : choices) {
						if (btn != b)
							btn.setSelection(false);
					}
										
					selectedOrder = (Order) b.getData();
					
					Event event = new Event();
					event.data = selectedOrder;
					notifyListeners(EVT_ORDER_SELETED, event);
				}
				
			});
		}
		
		addControlListener(new ControlAdapter() {

			@Override
			public void controlResized(ControlEvent e) {
				contentResized();
			}
			
		});
		
		currentPage = 0;
	}

	protected void contentResized() {
		if (inResize)
			return;
		
		inResize = true;
		int width = getSize().x;
		choicePerPage = (width - 2 * HORIZONTAL_PADDING - X_GAP) / (BUTTON_DIM + X_GAP);
		if (choicePerPage > MAX_CHOICE_PER_PAGE)
			choicePerPage = MAX_CHOICE_PER_PAGE;
		
		System.out.println("222Choice per page: " + choicePerPage);
		currentPage = 0;
		updateChoices();
		inResize = false;
	}

	@SuppressWarnings("serial")
	public void updateChoices() {
		ObjectContainer oc = Db4oContainerFactory.getDB();
		
		ObjectSet<Order> result = oc.query(new Predicate<Order>() {

			@Override
			public boolean match(Order order) {
				return order.getState() == Order.OrderState.ONHOLD;
			}
		}, new QueryComparator<Order>() {

			@Override
			public int compare(Order arg0, Order arg1) {
				return arg0.getDate().compareTo(arg1.getDate());
			}
		});
		
		int start_idx = currentPage * choicePerPage;
		int cur_idx = 0;
		
		while (result.hasNext() && cur_idx < start_idx + choicePerPage) {
			Order order = result.next();
			
			if (cur_idx >= start_idx) {
				choices[cur_idx-start_idx].setText(String.format("%1$TH:%1$TM:%1$TS", order.getDate()));
				choices[cur_idx-start_idx].setVisible(true);
				choices[cur_idx-start_idx].setLayoutData(String.format("width %1$d:%1$d:%1$d, height 50", BUTTON_DIM));
				choices[cur_idx-start_idx].setData(order);
				
				if (order == selectedOrder)
					choices[cur_idx-start_idx].setSelection(true);
				else
					choices[cur_idx-start_idx].setSelection(false);
			}
			
			++cur_idx;
		}
		
		for (; cur_idx < start_idx + choicePerPage; ++cur_idx) {
			choices[cur_idx-start_idx].setLayoutData("hidemode 3");
			choices[cur_idx-start_idx].setText("");
			choices[cur_idx-start_idx].setVisible(false);
			choices[cur_idx-start_idx].setData(null);
			choices[cur_idx-start_idx].setSelection(false);
		}
		
		selectedOrder = null;
		layout();
	}

	public Order getSelectedOrder() {
		return selectedOrder;
	}
	
	public void setSelectedOrder(Order order) {
		selectedOrder = order;
	}
}
